package com.swyc.osschat.Test.download;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @description 下载网络资源
 * @Author: z156940l@outlook.com
 * @className Test
 * @Date: 2022/8/4 15:48
 */
public class Test {

    public static void main(String[] args) throws Exception {
        // 1.下载地址
        URL url = new URL("https://cn.pornhub.com/view_video.php?viewkey=ph6358eef4b7d44");
        // 2.连接到这个资源
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        Map map = new HashMap();
        Map map1 = new Hashtable();
        InputStream inputStream = urlConnection.getInputStream();
        String file = url.getFile();
        int i = file.lastIndexOf("/");
        String fileName = file.substring(i+1);
        FileOutputStream fos = new FileOutputStream(fileName);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1){
            fos.write(buffer, 0, len);
        }
        fos.close();
        inputStream.close();
        urlConnection.disconnect();
    }
}
